#coding:utf8
from config import *
from config_mouse_start import *
from routin_simple import *

def switch_show_state():
    cfg.show_state = not cfg.show_state
def swith_show_last_m():
    cfg.show_last_m = not cfg.show_last_m
def switch_x2_mode():
    if not cfg.x2_mode:
        into_x2_mode()
    else:
        cfg.x2_mode = False
        clear(True)
def switch_force_tip_show():
    cfg.force_tip_no_show = not cfg.force_tip_no_show
def switch_auto_to_scale():
    cfg.auto_to_scale = not cfg.auto_to_scale
def enable_jump_task():
    cfg.enable_jump_task = True
def disable_jump_task():
    cfg.enable_jump_task = False
def switch_auto_jump():
    cfg.disable_auto_jump = not cfg.disable_auto_jump

pause_m = {
    '0.10':switch_show_jiao,
    '0.11':switch_auto_to_scale,
    '0.20':switch_show_pos,
    '0.2':minu_showmode,    
    '0.22':swith_show_last_m,
    '0.3':clear_mouse,
    '0.4':switch_scroll,
    '0.5':switch_left_middle_off,
    '0.55':switch_mouse_tip_l2,
    '0.66':switch_force_tip_show,
    '0.7':switch_show_pos,
    '0.77':enable_jump_task,
    '0.78':disable_jump_task,
    '0.8':switch_show_zi_enable,
    '0.88':switch_auto_jump,        
    '0.9':switch_show_state,    
    '0.99':switch_x2_mode,    
}

def is_in_pause_m(s):
    return s in pause_m

def pause_cmd():
    if cfg.sum not in pause_m:
        return False
    pause_m[cfg.sum]()
    set_cfg_sum_0()
    return True

def deal_pause():
    if cfg.x2_mode and cfg.sum!='0.' and not is_ff(cfg.sum):
        x2_jump()
        cfg.click2_time = time.time()
        return
    
    if cfg.w2showing and cfg.sum!='0.' and not is_ff(cfg.sum): 
        if cfg.sum=='0':
            ctrl2()
        else:
            deal_e(True)
        cfg.click2_time = time.time()
        return
    
    if pause_cmd():        
        print('pause hit')
        return             
        
    ss = cfg.sum        
    a = int(float(cfg.sum))
    if a>0:
        move_p(a)
        if ss[-2:]=='.0':
            cfg.click2_time = time.time()

        elif '0.' not in ss and ss[-1]!='.':            
            cfg.click_time = time.time()
        return
    
    mouse.Controller().click(mouse.Button.left, 2)

def valid_scroll_num():
    if cfg.sum=='0':
        return False
    
    if is_ff(cfg.sum):
        return True
    
    if float(cfg.sum)<100:
        return True
    return False

def scroll_change_sum():
    a,b = change_sum_10(cfg.sum)
    cfg.pause_mode = a
    cfg.sum = str(b)     
      
def scroll_fire():
    cfg.show_one_on =True
    x, y = get_cur_pos()
    idx = get_screen_indx(x, y)
    aa,b,c,d =  screen_config_for_indx[idx]    
    a = min(9,float(cfg.sum))
    g = 0
    if a == 9:
        g = 5 

    if cfg.pause_mode:
        dx = round(( c-aa) / 9 * a+aa)
        move_and_set_fix(dx - g, y)
        
    else:
        dy = round((d-b)/9*a+b)
        move_and_set_fix(x ,dy- g)        

def do_scroll_release(is_real=True):    
    if cfg.tt_co>right_jump_thre:
        cfg.tt_co=0
        return
    if str(cfg.sum)=='0' and (cfg.w2showing):
        ctrl2()
        return
    if str(cfg.sum)=='0' and not cfg.w2showing and cfg.min_ge in (0,1,3):
        if is_acc_up():
            acc_down(True)
            if is_real:
                return
        if (cfg.left_sign==0 or cfg.right_sign==0) and is_real:
            if cfg.right_sign==0 and kuai_ge_chg:
                cfg.right_sign=1
                # enter_ge_mode(3)
                # cfg.getokuai=100
                deal_pressed()
                return
            cfg.left_sign=1
            cfg.right_sign=1
            enter_ge_mode(1)
            cfg.getokuai=False
            return
                
        if cfg.left_sign==0:
            cfg.left_sign=1
            no_to_3()
            return      
        if is_real and cfg.min_ge==1:
            set_to_last(True)
            no_to_3()
            return
        if cfg.man_sign==0:
            cfg.man_sign=1
            no_to_3()
            return        
        
        if  is_real :          
            if is_qu():
                change_left_jump()
                return              
            if cfg.jump_state!=0:
                jump_state_chg(0)
            if cfg.jump_kuai==0:
                pp = get_cur_pos()                
                cfg.last_m = (pp[0]-cfg.fix_pos[0],pp[1]-cfg.fix_pos[1] )   
                enter_ge_mode(1)
                return
                                
            if cfg.jump_kuai!=0:
                acc_total()
                return
            acc_total()

            if cfg.jump_state==0 and cfg.jump_kuai!=0:                
                acc_total()
                return
            
        if not is_real and cfg.jump_state==0 and cfg.jump_kuai==2:
            cfg.jump_kuai=1
            return
            
        if not is_real and cfg.left_sign==0 and cfg.jump_state==1 and cfg.can_break:
            cfg.can_break=False
            cfg.left_sign=1
            return
        
    if not cfg.w2showing and cfg.sum=='0.':
        pause_xing()
        set_cfg_sum_0()
        return
    
    if not cfg.w2showing and valid_scroll_num() and is_no_move() and cfg.force_show:
            scroll_change_sum()
            scroll_fire()
            set_cfg_sum_0()
            return
    if '.' in cfg.sum and cfg.sum[-1]!='.':
        if not cfg.w2showing:
            movexy2()
            return
        movexy()
        return
    if (cfg.sum[-1]=='.' and len(cfg.sum) in (2,3) )  or \
        ('.' not in cfg.sum and  len(cfg.sum) in (2,1) and cfg.sum!='0'):
        d = False
        if cfg.sum[-1]=='.':
            d = True
            cfg.sum = cfg.sum[:-1]
        if cfg.w2showing or not d:
            if not cfg.w2showing:
                dotdot()
            movexy(True)
        else:
            movexy2(True)
        return            

    if not cfg.w2showing:                                  
        if not is_real:
            if cfg.man_sign==0:
                cfg.man_sign=1
                no_to_3()
                return            
            jump_state_to_man()
            cfg.is_after_man=True
            cfg.man_sign=0
            return          
                        
        no_to_3()
        return
    
    if cfg.sum!='0':            
        if  not cfg.w2showing:
            clear(True)
            cfg.force_show = True
            cfg.show_one_on = True                
            return

        xing()
        return
            
def deal_scroll_pressed(st, a):    
    if 'ctrl' in st:
        print('scroll exit')
        cfg.to_exit = True
        return
     
    if 'caps_lock' in st:
        cfg.pause_move = get_cur_pos()
        return
    
    if a == 15:        
        cfg.move_to_win = True
        return
    if a==16:
        cfg.sum = cfg.sum[0]

    if a==12:
        set_cfg_sum_0()
        cfg.pause_mode = not cfg.pause_mode
        return
    
    if a==14:
        if len(cfg.sum)>=2:
            cfg.sum = cfg.sum[0:-1]
        return
    
    if a==11:
        set_cfg_sum_0()
        cfg.pause_mode = not cfg.pause_mode
        return
        
    if a == 13:
        pause_xing()
        return
    
    if (a >= 0 and a <= 10):
        if len(cfg.sum)==1 and a!=10:
            cfg.sum = str(a)
        else:
            cfg.sum = deal_sum(cfg.sum, a, sum_max_l)
        scroll_fire()        